/* eslint-disable prefer-const */
const myDateTime = class DataTime {
  // 时间戳转换成YYYY-MM-DD hh:mm:ss
  getDateByMillisecond(time) {
    const d = new Date(time)
    let year, month, day, hour, minute, second
    year = d.getFullYear()
    month = this.addTimeFilter(d.getMonth() + 1)
    day = this.addTimeFilter(d.getDate())
    hour = this.addTimeFilter(d.getHours())
    minute = this.addTimeFilter(d.getMinutes())
    second = this.addTimeFilter(d.getSeconds())
    return `${year}-${month}-${day}:${hour}:${minute}:${second}`
  }
  // 获取YYYY-MM-DD hh:mm:ss
  getDateAll() {
    const d = new Date()
    let year, month, day, hour, minute, second
    year = d.getFullYear()
    month = this.addTimeFilter(d.getMonth() + 1)
    day = this.addTimeFilter(d.getDate())
    hour = this.addTimeFilter(d.getHours())
    minute = this.addTimeFilter(d.getMinutes())
    second = this.addTimeFilter(d.getSeconds())
    return `${year}-${month}-${day} ${hour}:${minute}:${second}`
  }
  // 获取本地时间年月日
  getDateTime(date) {
    const d = date === undefined ? new Date() : new Date(date)
    let year, month, day
    year = d.getFullYear()
    month = this.addTimeFilter(d.getMonth() + 1)
    day = this.addTimeFilter(d.getDate())
    return `${year}-${month}-${day}`
  }
  // 获取本地年月
  getMonthDateTime() {
    const d = new Date()
    let year, month
    year = d.getFullYear()
    month = this.addTimeFilter(d.getMonth() + 1)
    return `${year}-${month}`
  }
  // 获取当年
  getMyYear() {
    const d = new Date()
    let year
    year = d.getFullYear()
    return `${year}`
  }
  // 获取本地年月 201708
  getCurrentYearMonth() {
    const d = new Date()
    let year, month
    year = d.getFullYear()
    month = this.addTimeFilter(d.getMonth() + 1)
    return `${year}${month}`
  }
  getSecondDate(date) {
    const d = date === undefined ? new Date() : new Date(date)
    let day, hour, minute, second
    day = d.getDate()
    hour = d.getHours()
    minute = d.getMinutes()
    second = d.getSeconds()
    return second + minute * 60 + hour * 3600 + day * 3600 * 24
  }
  // 对比参数时间(08:00),若小于，则返回昨天的年月日，否则返回今天的年月日
  dayDateTime(date) {
    let d = new Date()
    let year, month, day, hours, minutes
    year = d.getFullYear()
    month = this.addTimeFilter(d.getMonth() + 1)
    day = this.addTimeFilter(d.getDate())
    hours = this.addTimeFilter(d.getHours())
    minutes = this.addTimeFilter(d.getMinutes())
    const hourMinutes = hours + ':' + minutes
    if (hourMinutes > date) {
      return `${year}${month}${day}`
    } else {
      d = new Date(d - 24 * 60 * 60 * 1000)
      year = d.getFullYear()
      month = this.addTimeFilter(d.getMonth() + 1)
      day = this.addTimeFilter(d.getDate())
      return `${year}${month}${day}`
    }
  }
  // 对比参数时间(08:00),若小于，则返回昨天的年月，否则返回今天的年月
  monthDateTime(date) {
    let d = new Date()
    let year, month, hours, minutes
    year = d.getFullYear()
    month = this.addTimeFilter(d.getMonth() + 1)
    hours = this.addTimeFilter(d.getHours())
    minutes = this.addTimeFilter(d.getMinutes())
    const hourMinutes = hours + ':' + minutes
    if (hourMinutes < date) {
      d = new Date(d - 24 * 60 * 60 * 1000)
      year = d.getFullYear()
      month = this.addTimeFilter(d.getMonth() + 1)
      return `${year}${month}`
    } else {
      return `${year}${month}`
    }
  }
  // 计算两个日期之间的相距天数
  dateDiff(sDate1, sDate2) {
    var oDate1, oDate2, iDays
    oDate1 = new Date(sDate1)
    oDate2 = new Date(sDate2)
    iDays = parseInt(Math.ceil(oDate1 - oDate2) / 1000 / 60 / 60 / 24)
    return iDays
  }
  getLocalTime() {
    const d = new Date()
    const year = d.getFullYear()
    const month = d.getMonth() + 1
    const day = d.getDate()
    const hours = this.addTimeFilter(d.getHours())
    const minutes = this.addTimeFilter(d.getMinutes())
    const seconds = this.addTimeFilter(d.getSeconds())
    return `${year}年${month}月${day}日 ${hours}:${minutes}:${seconds}`
  }
  addTimeFilter(args) {
    return args < 10 ? '0' + args : args
  }
  // 秒转时分秒
  formatSeconds(second) {
    var hh = parseInt(second / 3600)
    if (hh < 10) hh = '0' + hh
    var mm = parseInt((second - hh * 3600) / 60)
    if (mm < 10) mm = '0' + mm
    var ss = parseInt((second - hh * 3600) % 60)
    if (ss < 10) ss = '0' + ss
    var length = hh + ':' + mm + ':' + ss
    if (second > 0) {
      return length
    } else {
      return '00:00:00'
    }
  }
  // 获取上个月的最后一天
  getLastThreeDay() {
    const date = new Date()
    const year = date.getFullYear()
    const month = this.addTimeFilter(date.getMonth() + 1)
    var lastDay = new Date(year, month - 1, 0).getDate()
    return `${lastDay}`
  }
  // 获取本地年月 20170801
  getCurrentYearMonthDay() {
    const d = new Date()
    let year, month, day
    year = d.getFullYear()
    month = this.addTimeFilter(d.getMonth() + 1)
    day = this.addTimeFilter(d.getDate())
    return `${year}${month}${day}`
  }
  getYMD() {
    var d = new Date()
    var year = d.getFullYear()
    var month = d.getMonth() + 1
    if (month >= 1 && month <= 9) {
      month = '0' + month
    }
    var dt = d.getDate()
    if (dt >= 1 && dt <= 9) {
      dt = '0' + dt
    }
    var today = year + '-' + month + '-' + dt
    return today
  }
  getYM() {
    var d = new Date()
    var year = d.getFullYear()
    var month = d.getMonth() + 1
    if (month >= 1 && month <= 9) {
      month = '0' + month
    }
    var dt = d.getDate()
    if (dt >= 1 && dt <= 9) {
      dt = '0' + dt
    }
    var today = year + month
    return today
  }
  getDayCount() {
    var today = this.getYMD()
    var day = new Date(parseInt(today.substring(0, 4)), parseInt(today.substring(5, 7)), 0)
    var daycount = day.getDate()
    return daycount
  }
}
export default myDateTime
